package day4ClassAndArrayList;

import java.util.ArrayList;
import java.util.Scanner;

public class Students {
    public static void main(String[] args) {

        ArrayList<Students> arr = new ArrayList<Students>();
        Students s = new Students();

        s.name = "王五";
        s.age = 18;
        s.number = 101;
        arr.add(s);
        Students w = new Students();

        w.name = "张三";
        w.age = 19;
        w.number = 102;
        arr.add(w);

        Students z = new Students();
        z.name = "李四";
        z.age = 20;
        z.number = 103;
        arr.add(z);


        while (true){
            int choose = chooseFunction();
            System.out.println(choose);
            switch (choose){
                case 1:
                    addStudent(arr);
                    break;
                case 2:
                    update(arr);
                    break;
                case 3:
                    printStudent(arr);
                    break;
                case 4:
                    search(arr);
                    break;
                case 5:
                    exit();
                    return;
                default:
                    System.out.println("----------------------------------");
                    System.out.println("功能选择有误，请输入正确的功能序号!");
                    break;
            }
        }
        }


        public static int chooseFunction () {
            System.out.println("-------------学生管理系统------------");
            System.out.println("1.新增同学信息");
            System.out.println("2.更新所有同学的年龄信息");
            System.out.println("3.展示所有同学信息");
            System.out.println("4.输入一个学生学号，展示当前学号的学生所有的信息");
            System.out.println("5.退出系统");
            System.out.println("请输入要执行的操作序号：");
//接收键盘输入的功能选项序号
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();
            return choose;
        }


        //退出
        public static void exit () {
            System.out.println("----------------退出---------------");
            System.out.println("您已退出系统");
        }



        public static void printStudent (ArrayList < Students > arr) {
            for (int i = 0; i < arr.size(); i++) {
                Students s = arr.get(i);
                System.out.println("姓名：" + s.name + ",年龄：" + s.age + ",学号" + s.number);
            }
        }
        public static void addStudent (ArrayList < Students > arr){
            Scanner sc = new Scanner(System.in);
            Students s = new Students();
            System.out.println("存储学生姓名：");
            s.name = sc.next();
            System.out.println("存储学生年龄：");
            s.age = sc.nextInt();
            System.out.println("存储学生学号：");
            s.number = sc.nextInt();

            arr.add(s);
        }

        //更新年龄
    public static void update(ArrayList < Students > arr){
        System.out.println("------------更新年龄信息-----------");
        for (int i = 0; i < arr.size(); i++) {
            Students item = arr.get(i);
            System.out.println("请输入"+ item.name +"年龄");
            item.age = new Scanner(System.in).nextInt();
            arr.set(i, item);
        }
    }
    public static void search(ArrayList < Students > arr){
        System.out.println("请输入要查询的学号");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();

        for (int i=0;i<arr.size();i++){
            Students item = arr.get(i);
            if(x==item.number){
                Students items = arr.get(i);
                System.out.println("查询到的学生姓名为:"+items.name);
                System.out.println("查询到的学生年龄为:"+items.age);
                System.out.println("查询到的学生学号为:"+items.number);

            }
        }
    }

        public String name;
        public int number;
        public int age;

}